/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.io.Serializable;
import java.sql.*;
import java.util.ArrayList;

/**
 *
 * @author thien_000
 */
public class bean_nhanvien implements Serializable {

    private String MaNV;
    private String MatKhau;
    private String TaiKhoan;
    private String HoTen;
    private String ChucVu;
    private String GioiTinh;
    private String SoDT;

    public bean_nhanvien() {
    }

    public bean_nhanvien(String manv, String taikhoan, String matkhau, String hoten, String chucvu, String gioitinh, String sodt) {
        this.MaNV = manv;
        this.TaiKhoan = taikhoan;
        this.MatKhau = matkhau;
        this.HoTen = hoten;
        this.ChucVu = chucvu;
        this.GioiTinh = gioitinh;
        this.SoDT = sodt;
    }

    public bean_nhanvien(String taikhoan, String matkhau, String hoten, String chucvu, String gioitinh, String sodt) {
        this.TaiKhoan = taikhoan;
        this.MatKhau = matkhau;
        this.HoTen = hoten;
        this.ChucVu = chucvu;
        this.GioiTinh = gioitinh;
        this.SoDT = sodt;
    }

    public bean_nhanvien(String taikhoan) {
        this.TaiKhoan = taikhoan;
    }

    public String getMaNV() {
        return MaNV;
    }

    public void setMaNV(String MaNV) {
        this.MaNV = MaNV;
    }

    public String getTaiKhoan() {
        return TaiKhoan;
    }

    public void setTaiKhoan(String TaiKhoan) {
        this.TaiKhoan = TaiKhoan;
    }

    public String getHoTen() {
        return HoTen;
    }

    public void setHoTen(String HoTen) {
        this.HoTen = HoTen;
    }

    public String getChucVu() {
        return ChucVu;
    }

    public void setChucVu(String ChucVu) {
        this.ChucVu = ChucVu;
    }

    public String getGioiTinh() {
        return GioiTinh;
    }

    public void setGioiTinh(String GioiTinh) {
        this.GioiTinh = GioiTinh;
    }

    public String getSoDT() {
        return SoDT;
    }

    public void setSoDT(String SoDT) {
        this.SoDT = SoDT;
    }

    /**
     * @return the MatKhau
     */
    public String getMatKhau() {
        return MatKhau;
    }

    /**
     * @param MatKhau the MatKhau to set
     */
    public void setMatKhau(String MatKhau) {
        this.MatKhau = MatKhau;
    }

    public bean_nhanvien[] hienthinhanvien() {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = Connect.GetConnect();
            String sql = "SELECT * FROM nhanvien";
            stm = con.prepareStatement(sql);
            rs = stm.executeQuery();
            ArrayList list = new ArrayList();

            while (rs.next()) {
                String manv = rs.getString("MaNV");
                String taikhoan = rs.getString("TaiKhoan");
                String matkhau = rs.getString("MatKhau");
                String hoten = rs.getString("HoTen");
                String chucvu = rs.getString("ChucVu");
                String gioitinh = rs.getString("GioiTinh");
                String sodt = rs.getString("SoDT");
                bean_nhanvien nv = new bean_nhanvien(manv, taikhoan, matkhau, hoten, chucvu, gioitinh, sodt);
                list.add(nv);
            }
            bean_nhanvien[] result = new bean_nhanvien[list.size()];
            list.toArray(result);
            rs.close();
            stm.close();
            con.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public bean_nhanvien[] searchLikeTaiKhoan(String TaiKhoan) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = Connect.GetConnect();
            String sql = "SELECT * FROM nhanvien WHERE TaiKhoan LIKE ?";
            stm = con.prepareStatement(sql);
            stm.setString(1, "%" + TaiKhoan + "%");
            rs = stm.executeQuery();
            ArrayList list = new ArrayList();

            while (rs.next()) {
                String manv = rs.getString("MaNV");
                String taikhoan = rs.getString("TaiKhoan");
                String matkhau = rs.getString("MatKhau");
                String hoten = rs.getString("HoTen");
                String chucvu = rs.getString("ChucVu");
                String gioitinh = rs.getString("GioiTinh");
                String sodt = rs.getString("SoDT");
                bean_nhanvien nv = new bean_nhanvien(manv, taikhoan, matkhau, hoten, chucvu, gioitinh, sodt);
                list.add(nv);
            }
            bean_nhanvien[] result = new bean_nhanvien[list.size()];
            list.toArray(result);
            rs.close();
            stm.close();
            con.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean insert(String manv, String taikhoan, String matkhau,
            String hoten, String gioitinh, String chucvu, String sodt) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "INSERT INTO nhanvien(manv,taikhoan,matkhau,hoten,chucvu,gioitinh,sodt)" + "VALUES(?,?,?,?,?,?,?)";
            stm = con.prepareStatement(sql);
            stm.setString(1, manv);
            stm.setString(2, taikhoan);
            stm.setString(3, matkhau);
            stm.setString(4, hoten);
            stm.setString(5, chucvu);
            stm.setString(6, gioitinh);
            stm.setString(7, sodt);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteNhanVien() {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "DELETE FROM nhanvien WHERE TaiKhoan=?";
            stm = con.prepareStatement(sql);
            stm.setString(1, TaiKhoan);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateNhanVien() {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "UPDATE nhanvien SET MatKhau=?,HoTen=?,ChucVu=?,GioiTinh=?,SoDT=? WHERE TaiKhoan=?";
            stm = con.prepareStatement(sql);
            stm.setString(1, MatKhau);
            stm.setString(2, HoTen);
            stm.setString(3, ChucVu);
            stm.setString(4, GioiTinh);
            stm.setString(5, SoDT);
            stm.setString(6, TaiKhoan);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteAll() {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "DELETE FROM nhanvien";
            stm = con.prepareStatement(sql);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
